package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test6 {
    public static void main(String[] args) throws Exception {
        File dir = new File(
                Test6.class.getResource(".").toURI()
        );
        String packageName = Test6.class.getPackage().getName();

        File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : files) {
            String className = file.getName().substring(0, file.getName().indexOf("."));
            Class<?> cls = Class.forName(packageName + "." + className);

            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(AutoRunMethod.class)) {
                        AutoRunMethod annotation = method.getAnnotation(AutoRunMethod.class);
                        int value = annotation.value();
                        for (int i = 0; i < value; i++) {
                            method.invoke(cls.newInstance());
                        }
                    }
                }
            }
        }
    }
}
